<template>
  <a-modal
    :destroy-on-close="true"
    :mask-closable="false"
    :visible="visible"
    :title="title"
    :confirm-loading="loading"
    @cancel="handleClose"
    @ok="handleSubmit"
  >
    <a-form ref="formRef" :model="formData" :rules="formRules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="轮播图名称" name="bannerName">
        <a-input v-model:value="formData.bannerName" placeholder="请输入轮播图名称" />
      </a-form-item>
      <a-form-item label="轮播图" name="imageId">
        <select-image v-model:value="formData.imageId" :default-list="image ? [image] : []" />
      </a-form-item>
      <a-form-item label="链接地址" name="link">
        <a-input v-model:value="formData.link" placeholder="请输入链接地址" />
      </a-form-item>
      <a-form-item label="链接参数" name="params">
        <a-input v-model:value="formData.params" placeholder="请输入链接参数" />
      </a-form-item>
      <a-form-item label="状态" name="status" extra="用户端是否展示">
        <a-radio-group v-model:value="formData.status" button-style="solid">
          <a-radio-button v-for="item in ShowStatusEnum.$options()" :key="item.value" :value="item.value">{{
            item.label
          }}</a-radio-button>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="排序" name="sort" extra="数字越小越靠前">
        <a-input-number v-model:value="formData.sort" :min="0" :max="99" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script name="ContentBannerEdit" lang="ts" setup>
import { getBanner, saveBanner, updateBanner } from "@/api/content/banner";
import { enum2Value, toImage } from "@/utils/conver";
import { ShowStatusEnum } from "@/enums/common";

const emit = defineEmits(["reload"]);

const title = ref("添加轮播图");
const loading = ref(false);
const visible = ref(false);

const formRef = ref();
const formData: any = reactive({
  bannerId: undefined
});

const formRules = reactive({
  bannerName: [{ required: true, trigger: "blur", message: "请输入banner名称" }],
  imageId: [
    {
      required: true,
      min: 1,
      type: "number",
      trigger: "blur",
      message: "请选择Banner图片"
    }
  ],
  sort: [
    {
      required: true,
      type: "number",
      trigger: "blur",
      message: "请输入排序"
    }
  ]
});
const image = ref();

const handleGetBanner = (id: number) => {
  loading.value = true;
  getBanner(id)
    .then(response => {
      Object.assign(formData, enum2Value(response.data));
      image.value = toImage(formData.imageId, formData.imageUrl);
    })
    .finally(() => {
      loading.value = false;
    });
};

const handleAdd = () => {
  Object.assign(
    formData,
    reactive({
      bannerId: undefined,
      bannerName: "",
      imageId: 0,
      link: "",
      params: "",
      status: ShowStatusEnum.SHOW.code,
      sort: 99
    })
  );
  title.value = "添加轮播图";
  visible.value = true;
};

const handleEdit = (id: number) => {
  handleGetBanner(id);
  title.value = "编辑轮播图";
  visible.value = true;
};

const handleClose = () => {
  formData.bannerId = 0;
  formRef.value.resetFields();
  visible.value = false;
};

const handleSubmit = () => {
  formRef.value
    .validate()
    .then(() => {
      loading.value = true;
      if (formData.bannerId === undefined) {
        saveBanner(formData)
          .then(() => {
            antdMessage.success("添加成功");
            emit("reload");
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        updateBanner(formData)
          .then(() => {
            antdMessage.success("编辑成功");
            emit("reload");
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    })
    .catch(error => {
      console.log("error", error);
    });
};

defineExpose({ handleAdd, handleEdit });
</script>
